package com.sgb.goods.api;

import com.sgb.goods.BuildConfig;
import com.sgb.goods.utils.StringUtils;
import com.sgb.net.core.BaseCompatibleNet;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import okhttp3.OkHttpClient;

/**
 * 商品网络请求 网络请求 retrofit2 API
 *
 * @author cyj
 */
public class GoodsNet extends BaseCompatibleNet {

    private static class Single {
        private static final GoodsNet INSTANCE = new GoodsNet();
    }

    public static GoodsNet getInstance() {
        return Single.INSTANCE;
    }

    /**
     * 商品Api
     */
    private final GoodsApi goodsApi = getService(GoodsApi.class, BuildConfig.BASE_URL_SP);
    /**
     * 商品发布Api
     */
    private final GoodsPublishApi goodsPublishApi = getService(GoodsPublishApi.class, BuildConfig.BASE_URL_SP);
    /**
     * 资金中心Api
     */
    private final CapitalApi capitalApi = getService(CapitalApi.class, BuildConfig.BASE_URL_ZJ);
    /**
     * 询价Api
     */
    private final InquiryApi inquiryApi = getService(InquiryApi.class, BuildConfig.BASE_URL_SP);
    /**
     * 成员价格Api
     */
    private final MemberPriceApi memberPriceApi = getService(MemberPriceApi.class, BuildConfig.BASE_URL_SP);

    /**
     * Builder 网络配置处理
     * 关于网络请求 token 配置，作为单独运行时，handleBuilder 函数内容不要注释并设置 token ，反之，则全注释
     * okhttp3 问题
     *
     * @param builder
     * @see OkHttpClient.Builder    所有的基于okhttp3网络配置属性可以查看这里
     */
    @Override
    protected void handleBuilder(OkHttpClient.Builder builder) {
        if(BuildConfig.IS_MODULE_DEBUG) {
            builder.addInterceptor(chain -> {
                String token = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.resourcePCToken, "");
                MLog.d("TAG", "Bearer " + token);
                return chain.proceed(chain.request().newBuilder()
                        .addHeader("Authorization", StringUtils.isEmpty(token) ? "Basic Z2N0eDpnY3R4" : String.format("Bearer %s", token))
                        .addHeader("platform", "android")
                        .addHeader("Content-Type", "application/x-www-form-urlencoded")
                        .addHeader("type", "app-api-java")
                        .build());
            });
        }
    }

    /**
     * 获取商品Api接口对象
     *
     * @return GoodsApi
     */
    public GoodsApi getGoodsApi() {
        return goodsApi;
    }

    /**
     * 获取商品Api接口对象
     *
     * @return GoodsPublishApi
     */
    public GoodsPublishApi getGoodsPublishApi() {
        return goodsPublishApi;
    }

    /**
     * 获取资金中心Api接口对象
     *
     * @return CapitalApi
     */
    public CapitalApi getCapitalApi() {
        return capitalApi;
    }

    /**
     * 获取询价Api接口对象
     *
     * @return InquiryApi
     */
    public InquiryApi getInquiryApi() {
        return inquiryApi;
    }

    /**
     * 获取成员价格Api接口对象
     *
     * @return MemberPriceApi
     */
    public MemberPriceApi getMemberPriceApi() {
        return memberPriceApi;
    }
}
